package com.atguigu.leetcode;

import com.atguigu.leetcode.dataType.ListNode;

/**
 * 反转链表
 * @author xr-8
 * @date 2021-09-23
 * @time 22:56
 */
public class ReverseList {
    public static void main(String[] args) {
        ListNode node5 = new ListNode(5);
        ListNode node4 = new ListNode(4);
        ListNode node3 = new ListNode(3);
        ListNode node2 = new ListNode(2);
        ListNode node1 = new ListNode(1);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        reverseList(node1);
    }

    public static ListNode reverseList(ListNode node){
        ListNode cur = node;
        ListNode pre = null;
        while(cur!=null){
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        System.out.println(pre.val);
        return pre;
    }
}
